package bjut.core.factory;

import bjut.core.bean.BeanDefinition;
import bjut.core.bean.FactoryBean;
import bjut.core.exception.BeansException;
import bjut.core.factory.BeanFactory;
import bjut.core.postprocessor.BeanPostProcessor;
import bjut.core.resolver.StringValueResolver;
import bjut.core.singleton.DefaultSingletonBeanRegistry;
import bjut.core.singleton.FactoryBeanRegistrySupport;
import bjut.core.util.ClassUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhuzhiming
 * @Date: 2022/3/25 21:41
 */
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    List<BeanPostProcessor> beanPostProcessors = new ArrayList<>();
    private ClassLoader beanClassLoader = ClassUtil.getDefaultClassLoader();
    private final List<StringValueResolver> embeddedValueResolvers = new ArrayList<>();


    @Override
    public Object getBean(String beanName) {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return doGetBean(beanName, args);
    }

    @Override
    public <T> T getBean(String beanName, Class<T> requiredType) {
        return (T) getBean(beanName);
    }

    private <T> T doGetBean(final String beanName, final Object[] args) {
        Object shareInstance = getSingleton(beanName);
        if (shareInstance != null) {
            //看是否是 factory bean
            return (T) getObjectForBeanInstance(shareInstance, beanName);
        }

        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        Object bean = createBean(beanName, beanDefinition, args);

        return (T) getObjectForBeanInstance(bean, beanName);
    }

    private Object getObjectForBeanInstance(Object shareInstance, String beanName) {

        if (!(shareInstance instanceof FactoryBean)) {
            return shareInstance;
        }

        Object object = getCachedObjectForSingletonFactoryBean(beanName);
        if (null == object) {
            FactoryBean<?> factoryBean = (FactoryBean<?>) shareInstance;
            object = getObjectFromFactoryBean(factoryBean, beanName);
        }

        return object;

    }


    @Override
    public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        this.beanPostProcessors.remove(beanPostProcessor);
        this.beanPostProcessors.add(beanPostProcessor);
    }


    @Override
    public void addEmbeddedValueResolver(StringValueResolver valueResolver) {
        this.embeddedValueResolvers.add(valueResolver);
    }

    @Override
    public String resolveEmbeddedValue(String value) {

        String result = value;
        for (StringValueResolver resolver : this.embeddedValueResolvers) {
            result = resolver.resolverStringValue(result);
        }
        return result;
    }


    public List<BeanPostProcessor> getBeanPostProcessors() {
        return this.beanPostProcessors;
    }

    public ClassLoader getBeanClassLoader() {
        return this.beanClassLoader;
    }


    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
}
